﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Xml;

namespace BrainSculptor
{
    public partial class frmMain : Form
    {
        public frmMain()
        {
            InitializeComponent();
        }

        Shapes shape = Shapes.Circle;
        //int corners = 3;
        Color color = Color.Red;
        DateTime sessionEnd = DateTime.MinValue;
        DateTime sessionStart;
        SerializableDictionary<DateTime, List<int>> results = new SerializableDictionary<DateTime, List<int>>();
        SerializableDictionary<int, int> test = new SerializableDictionary<int, int>();

        private void frmMain_Load(object sender, EventArgs e)
        {
            pnlCustomColor.BackColor = Properties.Settings.Default.CustomColor;
            numSessionLength.Value = Properties.Settings.Default.SessionLenght;
            shape = (Shapes)Properties.Settings.Default.Shape;
            switch (shape)
            {
                case Shapes.Circle:
                    rbCircle.Checked = true;
                    break;
                case Shapes.Square:
                    rbRectangle.Checked = true;
                    break;
                case Shapes.Triangle:
                    rbTriangle.Checked = true;
                    break;
                case Shapes.Diamond:
                    rbDiamond.Checked = true;
                    break;
                case Shapes.Pentagon:
                    rbPentagon.Checked = true;
                    break;
                case Shapes.Hexagon:
                    rbHexagon.Checked = true;
                    break;
                case Shapes.Pentagram:
                    rbPentagram.Checked = true;
                    break;
                case Shapes.Hexagram:
                    rbHexagram.Checked = true;
                    break;
                case Shapes.Ring:
                    rbRing.Checked = true;
                    break;
                case Shapes.Frame:
                    rbFrame.Checked = true;
                    break;
                case Shapes.Cross:
                    rbCross.Checked = true;
                    break;
                case Shapes.X:
                    rbX.Checked = true;
                    break;
                default:
                    break;
            }
            switch (Properties.Settings.Default.Color)
            {
                case 1:
                    rbRed.Checked = true;
                    break;
                case 2:
                    rbGreen.Checked = true;
                    break;
                case 3:
                    rbBlue.Checked = true;
                    break;
                case 4:
                    rbYellow.Checked = true;
                    break;
                case 5:
                    rbCyan.Checked = true;
                    break;
                case 6:
                    rbMagenta.Checked = true;
                    break;
                case 7:
                    rbCustom.Checked = true;
                    break;
                default:
                    break;
            }
            if (!String.IsNullOrEmpty(Properties.Settings.Default.Results))
            {
                try
                {
                    string s = Properties.Settings.Default.Results;
                    StringReader sreader = new StringReader(Properties.Settings.Default.Results);
                    XmlReader reader = XmlReader.Create(sreader);
                    results.ReadXml(reader);
                }
                catch
                {
                    Properties.Settings.Default.Results = "";
                    Properties.Settings.Default.Save();
                }
            }
        }

        private void btnStart_Click(object sender, EventArgs e)
        {
            DateTime start = DateTime.Now;
            if (sessionEnd < DateTime.Now)
            {
                sessionEnd = DateTime.Now.AddMinutes(Properties.Settings.Default.SessionLenght);
                sessionStart = DateTime.Now;
            }
            frmShape frm = new frmShape(shape, color, sessionEnd);
            frm.ShowDialog();

            StringBuilder builder = new StringBuilder();
            XmlWriter writer = XmlWriter.Create(builder);

            if (results.ContainsKey(start))
                results[start].AddRange(frm.Results);
            else
                results.Add(start, frm.Results);
            results.WriteXml(writer);
            Properties.Settings.Default.Results = builder.ToString();
            Properties.Settings.Default.Save();
        }

        private void rbShape_CheckedChanged(object sender, EventArgs e)
        {
            if (rbCircle.Checked)
                shape = Shapes.Circle;
            else if (rbRectangle.Checked)
                shape = Shapes.Square;
            else if (rbTriangle.Checked)
                shape = Shapes.Triangle;
            else if (rbDiamond.Checked)
                shape = Shapes.Diamond;
            else if (rbPentagon.Checked)
                shape = Shapes.Pentagon;
            else if (rbHexagon.Checked)
                shape = Shapes.Hexagon;
            else if (rbPentagram.Checked)
                shape = Shapes.Pentagram;
            else if (rbHexagram.Checked)
                shape = Shapes.Hexagram;
            else if (rbRing.Checked)
                shape = Shapes.Ring;
            else if (rbFrame.Checked)
                shape = Shapes.Frame;
            else if (rbCross.Checked)
                shape = Shapes.Cross;
            else if (rbX.Checked)
                shape = Shapes.X;

            Properties.Settings.Default.Shape = (int)shape;
            Properties.Settings.Default.Save();
        }

        private void frmMain_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Escape)
                this.Close();
        }

        private void rbColor_CheckedChanged(object sender, EventArgs e)
        {
            if (rbRed.Checked)
            {
                color = Color.Red;
                Properties.Settings.Default.Color = 1;
            }
            else if (rbGreen.Checked)
            {
                color = Color.LimeGreen;
                Properties.Settings.Default.Color = 2;
            }
            else if (rbBlue.Checked)
            {
                color = Color.Blue;
                Properties.Settings.Default.Color = 3;
            }
            else if (rbYellow.Checked)
            {
                color = Color.Yellow;
                Properties.Settings.Default.Color = 4;
            }
            else if (rbCyan.Checked)
            {
                color = Color.Cyan;
                Properties.Settings.Default.Color = 5;
            }
            else if (rbMagenta.Checked)
            {
                color = Color.Magenta;
                Properties.Settings.Default.Color = 6;
            }
            else if (rbCustom.Checked)
            {
                color = Properties.Settings.Default.CustomColor;
                Properties.Settings.Default.Color = 7;
            }
            Properties.Settings.Default.Save();
        }


        private void btnCustomColor_Click(object sender, EventArgs e)
        {
            if (colorDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                Properties.Settings.Default.CustomColor = colorDialog.Color;
                pnlCustomColor.BackColor = colorDialog.Color;
                color = colorDialog.Color;
                Properties.Settings.Default.Save();
            }
        }

        private void numSessionLength_ValueChanged(object sender, EventArgs e)
        {
            int length = Convert.ToInt32(numSessionLength.Value);
            if (length != Properties.Settings.Default.SessionLenght)
            {
                Properties.Settings.Default.SessionLenght = length;
                Properties.Settings.Default.Save();
                sessionEnd = DateTime.MinValue;
            }
        }

        private void btnStatistics_Click(object sender, EventArgs e)
        {
            frmStatistics frm = new frmStatistics();
            frm.ShowDialog();
        }
    }


    public enum Shapes
    {
        Circle, Square, Triangle, Diamond, Pentagon, Hexagon, Pentagram, Hexagram, Ring, Frame, Cross, X
    }
}